package com.playball.lottery.frm.buyhall;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.playball.lottery.R;
import com.playball.lottery.ApplicationData;
import com.playball.lottery.customview.BallDTO;
import com.playball.lottery.customview.MyDialog;
import com.playball.lottery.customview.NewDelButtonAdapter;
import com.playball.lottery.customview.ShowTipDialog;
import com.playball.lottery.frm.yhzx.BetProtocolActivity;
import com.playball.lottery.frm.yhzx.JddRechargeListActivity;
import com.playball.lottery.frm.yhzx.LoginActivity;
import com.playball.lottery.nt.GetRequestParams;
import com.playball.lottery.util.Constants;
import com.playball.lottery.util.ConvertJsonToList;
import com.playball.lottery.util.DefConstants;
import com.playball.lottery.util.DynArrayInt;
import com.playball.lottery.util.GetIDAsyncLoader;
import com.playball.lottery.util.LotConstants;
import com.playball.lottery.util.MyPreference;
import com.playball.lottery.util.New11x5Util;
import com.playball.lottery.util.PreferencesUtils;
import com.playball.lottery.util.SysApplication;


public class Fc3dListActivity extends ListActivity implements DefConstants {
	/** 上下文内容 */
	private LinearLayout btnAddRandom;
	private LinearLayout btnBack;
	private LinearLayout btnClear;
	// private Button btnPlus;
	// private Button btnMinus;
	private Button btnBuy;
	/** 发起合买 */
	private Button btn_buy_fc3dhemai;
	// private TextView bsText;
	private EditText buy_pl3xhlb_et;
	private TextView moneyText;

	View buy_pl3xhlb_linearlayout01;
	View buy_pl3xhlb_linearlayout02;

	private int buy_icon;

	private int playTypeId;
	private int bs = 1;
	private int zh = 1; // 追号
	private int allMoney;
	private int lotID;
	// private String vouchersNo;// 折扣编号
	// private double canDeductMoney;// 折扣金额
	private String issueID;
	private String issueName;
	private String randomBall;

	private DynArrayInt ball_1st_dintInt;
	private DynArrayInt ball_2nd_dintInt;
	private DynArrayInt ball_3rd_dintInt;
	private DynArrayInt money_dintInt;

	private ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
	LinearLayout foot_layout;
	TextView tv_bet_protocol;
	CheckBox cb_bet_protocol;
	private LinearLayout yincangyu1, yincangyu2;
	private CheckBox iszhuihao;
	private TextView wuqi, shiqi, shiwuqi;
	private LinearLayout ceshi;

	private String[] dyn;

	private EditText zhuihao_EditText;
	private Context context;
	private PreferencesUtils pUtil;
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			Bundle b = msg.getData();
			issueID = b.getString("issueID");
			issueName = b.getString("issueName");

		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = this;
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.buy_fc3d_list);
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
						| WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
		SysApplication.getInstance().addActivity(this);
		pUtil = new PreferencesUtils(context, Constants.PREFERENCES_NAME);
		// 初始化
		initView();
		ceshi = (LinearLayout) findViewById(R.id.ceshi);
		LayoutInflater inflater = LayoutInflater.from(this);
		foot_layout = (LinearLayout) inflater.inflate(R.layout.bet_protocol_foot, null);
		tv_bet_protocol = (TextView) foot_layout.findViewById(R.id.bet_protocol);
		cb_bet_protocol = (CheckBox) foot_layout.findViewById(R.id.bet_protocol_choice);
//		tv_bet_protocol.setText(Html.fromHtml("<u>我已满18周岁并同意《服务条款》"));
		tv_bet_protocol.setVisibility(View.VISIBLE);//显示服务协议
		cb_bet_protocol.setChecked(true);
		tv_bet_protocol.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivityForResult(new Intent(Fc3dListActivity.this, BetProtocolActivity.class), 0);
			}
		});
		this.getListView().addFooterView(foot_layout);
		buy_pl3xhlb_et = (EditText) findViewById(R.id.buy_11x5xhlb_EditText01);
		buy_pl3xhlb_et.addTextChangedListener(textWatcher);

		// 获得传来的数组，并放入ListView
		Intent intent = getIntent();
		playTypeId = intent.getIntExtra("PlayTypeID", 0);
		allMoney = intent.getIntExtra("AllMoney", 0);
		issueID = intent.getStringExtra("IssueID");
		issueName = intent.getStringExtra("IssueName");
		lotID = intent.getIntExtra("LotID", LOT_ID_FC3D);
		moneyText.setText(allMoney + "");
		if (issueID == null || issueName == null) {
			GetIDAsyncLoader task = new GetIDAsyncLoader(this, handler, lotID);
			String[] parms = new String[2];
			parms[0] = "200";
			JSONObject jsob = new JSONObject();
			try {
				// 设置请求所需参数
				jsob.put("LottID", lotID);
			} catch (JSONException ex) {
				// TODO Auto-generated catch block
				Log.e("error", ex.toString());
			}
			parms[1] = jsob.toString();
			task.execute(parms);
		}

		// Log.d("com.playball.lottery", "playTypeId:" + playTypeId + "  allMoney:"
		// + allMoney + "  lotID:" + lotID );

		@SuppressWarnings("unchecked")
		List<BallDTO> arrayList = (ArrayList<BallDTO>) intent.getSerializableExtra("list_elvdto");
		for (BallDTO elvdto : arrayList) {
			// Log.e("com.playball.lottery", "elvdto:" + elvdto.getPl3_ball());
			String elv_ball = elvdto.getPl3_ball();
			list = getData(elv_ball);
		}
		initSimpleAdapter(list);

		// 继续添加号码
		btnBack.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.putExtra("isfromkaiJianglist", 2);
				intent.putExtra("playTypeId", playTypeId);
				intent.putExtra("ifbacklist", true);
				intent.setClass(context, Fc3dxhActivity.class);
				startActivity(intent);
				finish();
			}
		});

		// 清空列表按钮
		btnClear.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (ApplicationData.getInstance().getList_pl3dto().size() > 0) {
					MyDialog.Builder dialog = new MyDialog.Builder(Fc3dListActivity.this);
					dialog.setTitle("清空号码提示");
					dialog.setMessage("清空后您已选择的号码列表将会清空，确认继续?");
					dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// 获得自定义的应用程序BallDTO,初始化
							list = new ArrayList<HashMap<String, Object>>();
							ApplicationData.getInstance().initList();
							// 金额清零
							allMoney = 0;
							buy_pl3xhlb_et.setText("1");
							zhuihao_EditText.setText("1");
							bs = 1;
							/*
							 * 追号需要修改的地方
							 */
							zh = 1;
							setNoMsg();
							buy_pl3xhlb_linearlayout01.setVisibility(View.GONE);
							buy_pl3xhlb_linearlayout02.setVisibility(View.VISIBLE);
							initText();
							dialog.cancel();
						}
					});
					dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.cancel();
						}
					}).create().show();
				}
			}
		});

		// 添加随机号码
		btnAddRandom.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				buy_pl3xhlb_linearlayout01.setVisibility(View.VISIBLE);
				buy_pl3xhlb_linearlayout02.setVisibility(View.GONE);
				randomBall = "";
				ball_1st_dintInt = new DynArrayInt();
				ball_2nd_dintInt = new DynArrayInt();
				ball_3rd_dintInt = new DynArrayInt();
				int moneyAdd = 0;
				if (playTypeId == 602) {// 直选
					ball_1st_dintInt.addInt(RandomGenData(0, 9, 1/* , "first" */));
					ball_2nd_dintInt.addInt(RandomGenData(0, 9, 1/* , "second" */));
					ball_3rd_dintInt.addInt(RandomGenData(0, 9, 1/* , "third" */));
					String first = ball_1st_dintInt.getIntToStringForSingle(0);
					String second = ball_2nd_dintInt.getIntToStringForSingle(0);
					String third = ball_3rd_dintInt.getIntToStringForSingle(0);
					randomBall = first + "|" + second + "|" + third;
					moneyAdd = 2;
				} else if (playTypeId == 605) {// 组选三
					ball_1st_dintInt.addInt(RandomGenData(0, 9, 2/* , "first" */));
					randomBall = ball_1st_dintInt.getAllIntToStringForSingle(ball_1st_dintInt.getAllInt());
					randomBall = randomBall.substring(0, randomBall.length() - 1);
					moneyAdd = 4;
				} else if (playTypeId == 604) {// 组选六
					ball_1st_dintInt.addInt(RandomGenData(0, 9, 3/* , "first" */));
					randomBall = ball_1st_dintInt.getAllIntToStringForSingle(ball_1st_dintInt.getAllInt());
					randomBall = randomBall.substring(0, randomBall.length() - 1);
					moneyAdd = 2;
				}
				Log.d("com.playball.lottery", "btnAddRandom.setOnClickListener randomBall:" + randomBall);
				// 实例化一个新的BallDTO放入到全局变量ApplicationBall中的List_elv里

				BallDTO elvdto = new BallDTO();
				elvdto.setPl3_ball(randomBall + "-" + moneyAdd);
				// 获得自定义的应用程序BallDTO
				// 将我们放到进程中的全局变量拿出来，看是不是我们曾经设置的值
				Log.i("com.playball.lottery", "InitLabel:application_pl3_sel:"
						+ ApplicationData.getInstance().getList_pl3dto().size());
				// 新选的号码，追加在ApplicationBall全局变量list_ssqdto里
				ApplicationData.getInstance().getList_pl3dto().add(elvdto);
				allMoney += moneyAdd;
				ApplicationData.getInstance().setAll_pl3money(allMoney);
				ApplicationData.getInstance().setPlay_pl3Id(playTypeId);
				moneyText.setText(allMoney + "");
				Log.d("com.playball.lottery", "btnAddRandom.setOnClickListener ballInfact = " + randomBall);
				initSimpleAdapter(getData(randomBall + "-" + moneyAdd));
			}
		});

		// 购买按钮
		btnBuy.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (ApplicationData.getInstance().isUserLogin()) {
					if (issueID == null || issueName == null) {
						Toast.makeText(context, R.string.the_network_is_not_to_force, Toast.LENGTH_SHORT).show();
						return;
					}
					if(bs * allMoney > 2000000){
						Toast.makeText(context, "单个方案的总金额必须在2元至2000000元之间", Toast.LENGTH_SHORT).show();
						return;
					}
					
					if (list.size() < 1) {
						Toast.makeText(Fc3dListActivity.this, R.string.betting_number_cannot_be_empty,
								Toast.LENGTH_SHORT).show();
					} /*
					 * else if (buy_pl3xhlb_et.getText().toString().equals(""))
					 * { Toast.makeText(Fc3dListActivity.this,
					 * R.string.buy_at_least_one_times, Toast.LENGTH_SHORT)
					 * .show(); buy_pl3xhlb_et.setText("1"); } else if
					 * (zhuihao_EditText.getText().toString().equals("")) {
					 * zhuihao_EditText.setText("1");
					 * Toast.makeText(Fc3dListActivity.this,
					 * R.string.period_of_time_can_not_be_empty,
					 * Toast.LENGTH_SHORT).show(); }
					 */else {
						// Dialog/* AlertDialog.Builder */dialog = new
						// AlertDialog.Builder(
						// Fc3dListActivity.this)
						// .setTitle("购买提示")
						// .setMessage("福彩3D第" + issueName + "期 ，是否确认购买？")
						// .setIcon(R.drawable.yhzx_main_warn)
						//
						// .setPositiveButton("确定",
						// new DialogInterface.OnClickListener() {
						// @Override
						// public void onClick(
						// DialogInterface dialog,
						// int which) {
						// // startVouchersThread();
						// startBuyThread();
						// }
						// })
						// .setNegativeButton("取消",
						// new DialogInterface.OnClickListener() {
						// @Override
						// public void onClick(
						// DialogInterface dialog,
						// int which) {
						// dialog.cancel();
						// }
						// }).create();
						// dialog.setCanceledOnTouchOutside(false);
						// dialog.setCancelable(true);// 设置back键是否可以取消
						// dialog.show();

						if (cb_bet_protocol.isChecked() == false) {
							Toast.makeText(Fc3dListActivity.this, R.string.need_to_agree_to_just_can_continue_to_buy,
									Toast.LENGTH_SHORT).show();
							return;
						}

						/*
						 * 追号需要修改的地方
						 */
						if (zh == 1) {
							startBuyThread();
						} else {
							newZhuiHaoTthread();
						}
					}
				} else {
					Intent intent = new Intent();
					intent.setClass(context, LoginActivity.class);
					startActivity(intent);
				}
			}
		});

		// 发起合买
		btn_buy_fc3dhemai.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (ApplicationData.getInstance().isUserLogin()) {
					if (issueID == null || issueName == null) {
						Toast.makeText(context, R.string.the_network_is_not_to_force, Toast.LENGTH_SHORT).show();
						return;
					}
					// bs =
					// Integer.parseInt(buy_ssqxhlb_EditText1.getText().toString());
					if (list.size() < 1) {
						Toast.makeText(Fc3dListActivity.this, R.string.betting_number_cannot_be_empty,
								Toast.LENGTH_SHORT).show();
					} /*
					 * else if (buy_pl3xhlb_et.getText().toString().equals("")
					 * || Long.parseLong(buy_pl3xhlb_et.getText().toString()) ==
					 * 0) { buy_pl3xhlb_et.setText("1");
					 * Toast.makeText(Fc3dListActivity.this,
					 * R.string.buy_at_least_one_times, Toast.LENGTH_SHORT)
					 * .show(); } else if
					 * (zhuihao_EditText.getText().toString().equals("")) {
					 * zhuihao_EditText.setText("1");
					 * Toast.makeText(Fc3dListActivity.this,
					 * R.string.period_of_time_can_not_be_empty,
					 * Toast.LENGTH_SHORT).show(); }
					 */else {

						if (cb_bet_protocol.isChecked() == false) {
							Toast.makeText(Fc3dListActivity.this, R.string.need_to_agree_to_just_can_continue_to_buy,
									Toast.LENGTH_SHORT).show();
							return;
						}

						Intent intent = new Intent();
						intent.putExtra("list_elvdto", ApplicationData.getInstance().getList_pl3dto());
						intent.putExtra("AllMoney", ApplicationData.getInstance().getAll_pl3money());
						intent.putExtra("bs", bs);
						intent.putExtra("LotID", lotID);
						intent.putExtra("IssueID", issueID);
						intent.putExtra("IssueName", issueName);
						intent.putExtra("PlayTypeId", playTypeId);
						intent.setClass(Fc3dListActivity.this, FaQiHeMaiActivity.class);
						Fc3dListActivity.this.startActivityForResult(intent, REQ_CODE_BUYMAIN);
					}
				} else {
					Intent intent = new Intent();
					intent.setClass(context, LoginActivity.class);
					startActivity(intent);
				}
			}
		});
		// 软件盘
		ceshi.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

			@Override
			public void onGlobalLayout() {
				int heightDiff = ceshi.getRootView().getHeight() - ceshi.getHeight();
				// 判断焦点在哪里
				if (zhuihao_EditText.hasFocus()) {
					if (heightDiff > 100) {
						// 大小超过100时，一般为显示虚拟键盘事件
						yincangyu1 = (LinearLayout) findViewById(R.id.yincangyu1);
						yincangyu2 = (LinearLayout) findViewById(R.id.yicangyu2);
						yincangyu2.setVisibility(View.VISIBLE);
						wuqi = (TextView) findViewById(R.id.wuqi);
						shiqi = (TextView) findViewById(R.id.shiqi);
						shiwuqi = (TextView) findViewById(R.id.shiwuqi);
						wuqi.setOnClickListener(new OnClickListener() {

							@Override
							public void onClick(View v) {
								zh = 5;
								zhuihao_EditText.setText("5");
							}
						});
						shiqi.setOnClickListener(new OnClickListener() {

							@Override
							public void onClick(View v) {
								zh = 10;
								zhuihao_EditText.setText("10");
							}
						});
						shiwuqi.setOnClickListener(new OnClickListener() {

							@Override
							public void onClick(View v) {
								zh = 15;
								zhuihao_EditText.setText("15");
							}
						});
						if (zh > 1) {
							yincangyu1.setVisibility(View.VISIBLE);
						} else {
							yincangyu1.setVisibility(View.GONE);
						}
					} else {
						yincangyu1 = (LinearLayout) findViewById(R.id.yincangyu1);
						yincangyu2 = (LinearLayout) findViewById(R.id.yicangyu2);
						yincangyu1.setVisibility(View.GONE);
						yincangyu2.setVisibility(View.GONE);
					}
				}

			}
		});
	}

	public void onResume() {
		super.onResume();
		
	}

	public void onPause() {
		super.onPause();
		
	}

	// 追号的EditText监听
	private TextWatcher zhuihaoWatcher = new TextWatcher() {
		private boolean isChanged = false;

		@Override
		public void afterTextChanged(Editable s) {
			if (isChanged) {
				return;
			}
			if (!zhuihao_EditText.getText().toString().equals("")) {
				zh = Integer.parseInt(s.toString());
				isChanged = true;
				String str2 = s.toString().trim().replaceFirst("^0*", "");// 正则去除
				if (zh < 1) {
					// Toast.makeText(Fc3dListActivity.this,
					// R.string.buy_at_least_one, Toast.LENGTH_SHORT).show();
					zhuihao_EditText.setText("1");
					zh = 1;
				} else if (zh > 50) {
					Toast.makeText(Fc3dListActivity.this, R.string.after_fifty_most_period, Toast.LENGTH_SHORT).show();
					zhuihao_EditText.setText("50");
					zh = 50;
				} else {
					yincangyu1 = (LinearLayout) findViewById(R.id.yincangyu1);
					if (zh == 1) {
						yincangyu1.setVisibility(View.GONE);
					} else {
						yincangyu1.setVisibility(View.VISIBLE);
					}
					zhuihao_EditText.setText(str2);
				}
				zhuihao_EditText.setSelection(zhuihao_EditText.length());
				isChanged = false;
				zhuihao_EditText.invalidate();
			} else {
				/*
				 * if (zhuihao_EditText.getText().toString().equals("")) {
				 * Toast.makeText(Fc3dListActivity.this,
				 * R.string.period_of_time_can_not_be_empty, Toast.LENGTH_SHORT)
				 * .show();
				 * 
				 * }
				 */
				zh = 1;
			}
			if (zh == 1) {
				btn_buy_fc3dhemai.setEnabled(true);
				btn_buy_fc3dhemai.setBackgroundResource(R.drawable.ui_new_btn_yes_selector);
			} else {
				btn_buy_fc3dhemai.setEnabled(false);
				btn_buy_fc3dhemai.setBackgroundResource(R.drawable.ui_new_btn_cancel_normal);
			}
			moneyText.setText(allMoney * zh * bs + "");
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
		}

	};
	private TextWatcher textWatcher = new TextWatcher() {
		private boolean isChanged = false;

		public void afterTextChanged(Editable s) {
			// TODO Auto-generated method stub

			if (isChanged) {
				return;
			}
			if (!buy_pl3xhlb_et.getText().toString().equals("")) {

				bs = Integer.parseInt(s.toString());
				isChanged = true;
				String str2 = s.toString().trim().replaceFirst("^0*", "");// 正则去除
				if (bs < 1) {
					// Toast.makeText(Fc3dListActivity.this,
					// R.string.the_minimum_ratio_of_one,
					// Toast.LENGTH_SHORT).show();
					buy_pl3xhlb_et.setText("1");
					bs = 1;
				} else if (bs > 999) {
					Toast.makeText(Fc3dListActivity.this, R.string.the_maximum_ratio_of_dig_3_nine,
							Toast.LENGTH_SHORT).show();
					buy_pl3xhlb_et.setText("999");
					bs = 999;
				} else {
					buy_pl3xhlb_et.setText(str2);
				}
				buy_pl3xhlb_et.setSelection(buy_pl3xhlb_et.length());
				isChanged = false;
				buy_pl3xhlb_et.invalidate();
			} else {
				/*
				 * if (buy_pl3xhlb_et.getText().toString().equals("")) {
				 * Toast.makeText(Fc3dListActivity.this,
				 * R.string.buy_at_least_one_times, Toast.LENGTH_SHORT).show();
				 * 
				 * }
				 */
				bs = 1;
			}
			moneyText.setText(allMoney * bs * zh + "");
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			// TODO Auto-generated method stub
			Log.d("TAG", "beforeTextChanged--------------->");
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			Log.d("TAG", "onTextChanged--------------->");
		}

	};

	class AsyncLoader extends AsyncTask<String, Integer, String> {// 继承AsyncTask

		String action;
		ShowTipDialog showdialog = new ShowTipDialog();

		// 1.准备运行
		protected void onPreExecute() {
			showdialog.showDialog(Fc3dListActivity.this);
		}

		// 2.正在后台运行
		@Override
		protected String doInBackground(String... paramsArr) {
			// 处理后台执行的任务，在后台线程执行
			String strResult = null;

			action = paramsArr[1];
			// 获取请求返回值
			String UserID = ApplicationData.getInstance().getUserID();
			strResult = GetRequestParams.getRequestUserIDResult(paramsArr[0], UserID, action, getApplicationContext());
			// 获取请求返回值
			return strResult;
		}

		// 3.进度更新
		protected void onProgressUpdate(Integer... progress) {
		}

		// 4.完成后台任务
		protected void onPostExecute(String result) {
			if (result != null) {
				// if (action.equals("203")) {
				// getVouchers(result);
				// }
				try {
					JSONTokener jsonParser = new JSONTokener(result);
					JSONObject js = (JSONObject) jsonParser.nextValue();
					// 接下来的就是JSON对象的操作了
					if (Integer.parseInt(js.getString("Code")) == 0) {
						if (action.equals("207")) {
							Buy(result);
						}else {
							Buy2(result);
						}
					}else {
						Toast.makeText(Fc3dListActivity.this, js.getString("Data"), Toast.LENGTH_LONG).show();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}finally {
					showdialog.closeDialog();
				}
			} else {
				Toast.makeText(Fc3dListActivity.this, getString(R.string.problem_01), Toast.LENGTH_LONG).show();
			}
		}

		// 5.完成后操作
		protected void onCancelled() {
		}

	}

	// private void startVouchersThread() {
	// AsyncLoader task = new AsyncLoader();
	// String[] dyn = new String[2];
	// JSONObject params = new JSONObject();
	// try {
	// // 设置请求所需参数
	// params.put("Name", app_pl3dto.getLoginName());
	// params.put("Password", app_pl3dto.getLoginPwd());
	// params.put("LotID", lotID);
	// params.put("CastType", 0);
	// params.put("BuyMoney", bs * allMoney);
	// } catch (JSONException ex) {
	// // TODO Auto-generated catch block
	// Log.e("error", ex.toString());
	// }
	// dyn[0] = params.toString();
	// dyn[1] = "203";
	// task.execute(dyn);
	// }
	/*
	 * DOTO 追号需要修改的地方
	 */
	/**
	 * 获取ChaseIsuses列表
	 */
	public String getIsuses() {
		List list=new ArrayList();
		int endNumber=(150-Integer.parseInt(issueName.substring(4,issueName.length())));
		int issueNumber=Integer.parseInt(issueName);
		try{
			List<Map<String, Object>> list_LotNum = new ArrayList<Map<String, Object>>();
			list_LotNum = getLotNum();
			// 设置请求所需参数
			JSONArray array1 = new JSONArray();
			for (int i = 0; i < list_LotNum.size(); i++) {
				JSONObject o = new JSONObject(list_LotNum.get(i));
				array1.put(o);
			}
			if (zh<endNumber){
				for (int i = 0; i <zh ; i++) {
					JSONObject map= new JSONObject();
					map.put("IsuseName",issueNumber);
					map.put("LotteryNumber",array1);
					map.put("Multiple",bs);
					map.put("Money",bs * allMoney);
					issueNumber++;
					list.add( map);
				}
			}else {
				Toast.makeText(Fc3dListActivity.this, "还有"+endNumber+"可以追", Toast.LENGTH_LONG).show();
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return list.toString();
	}
	// 新追号线程
	private void newZhuiHaoTthread(){

		AsyncLoader taskIssure = new AsyncLoader();
		dyn = new String[2];
		JSONObject params = new JSONObject();
		try {
			params.put("LottID", lotID);
			params.put("StopMoney",iszhuihao.isChecked() ? 1 : 0);
			params.put("ChaseIsuses", getIsuses());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		dyn[0] = params.toString();
		dyn[1] = "208";
		taskIssure.execute(dyn);
	}
	// 追号线程
	private void startZhuiHaoThread() {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		list = getLotNum();

		AsyncLoader task = new AsyncLoader();
		dyn = new String[2];
		JSONObject params = new JSONObject();
		try {
			JSONArray array1 = new JSONArray();// 彩票
			for (int i = 0; i < list.size(); i++) {
				JSONObject o = new JSONObject(list.get(i));
				array1.put(o);
			}
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("LotteryNumber", array1);
			
			params.put("LottID", lotID);
			params.put("StopMoney", iszhuihao.isChecked() ? 1 : 0);
			params.put("ChaseCount", zh);
			params.put("IssueID", issueID);
			params.put("Money", bs * allMoney);
			params.put("Multiple", bs);
			params.put("BuyMoney", bs * allMoney * zh);
			params.put("ChaseIsuses", jsonObject);
		} catch (JSONException ex) {
			Log.e("error", ex.toString());
		}
		dyn[0] = params.toString();
		dyn[1] = "2082";
		task.execute(dyn);
	}

	private void startBuyThread() {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		list = getLotNum();

		AsyncLoader task = new AsyncLoader();
		dyn = new String[2];
		JSONObject params = new JSONObject();

		try {
			// 设置请求所需参数
			JSONArray array1 = new JSONArray();
			for (int i = 0; i < list.size(); i++) {
				JSONObject o = new JSONObject(list.get(i));
				// System.out.println(o);
				array1.put(o);
			}
			params.put("SchemeType", 1);// 方案类型 1自购，2合买，3保存 1
			params.put("BetType", 1);// 投注类型 1选号投注 2单式上传 3先发后传 1
			params.put("BonusScale", 0);
			params.put("AssureShare", 0);
			params.put("Description", "");
			params.put("OpenUserList", "");
			params.put("SecrecyLevel", 4);
			params.put("BuyShare", bs * allMoney);
			params.put("Multiple", bs);
			params.put("LotteryID", lotID);
			params.put("IssueID", issueID);
			params.put("IssueName", issueName);
			params.put("Number", array1);
			params.put("Money", bs * allMoney);
		} catch (JSONException ex) {
			// TODO Auto-generated catch block
			Log.e("error", ex.toString());
		}
		dyn[0] = params.toString();
		dyn[1] = "207";
		task.execute(dyn);
	}

	// public void getVouchers(String strResult) {
	// Map<String, Object> map = new HashMap<String, Object>();
	//
	// try {
	// JSONTokener jsonParser = new JSONTokener(strResult);
	// JSONObject js = (JSONObject) jsonParser.nextValue();
	// if (Integer.parseInt(js.getString("Code")) > 0) {
	// map = ConvertJsonToList.getMap(js.getString("Result"));
	// vouchersNo = map.get("VouchersNo").toString();
	// canDeductMoney = (double)
	// (map.get("CanDeductAmount").toString().equals("") ? 0.0 :
	// Double.parseDouble(map.get("CanDeductAmount").toString()));
	// } else {
	// Toast.LENGTH_SHORT).show();
	// }
	// } catch (JSONException e) {
	// // 异常处理代码
	// Log.e("error", e.toString());
	// }
	//
	// }

	public void Buy(String strResult) {
		try {
			JSONTokener jsonParser = new JSONTokener(strResult);
			JSONObject js = (JSONObject) jsonParser.nextValue();
			if (Integer.parseInt(js.getString("Code")) < 0) {
				buy_icon = R.drawable.buy_failure;
//				if (js.getString("Data").contains(Constants.BALANCE_BUZU)) {
//					Intent intent = new Intent();
//					intent.setClass(context, JddRechargeListActivity.class);
//					intent.putExtra("params", dyn[0]);
//					startActivityForResult(intent, REQ_CODE_NO_MONEY_2_RECHARGE);
//				}else{
					new ShowTipDialog().showBuyInfo(Fc3dListActivity.this, js.getString("Data"), "购买失败", buy_icon, dyn[0]);	
//				}
			} else {
				buy_icon = R.drawable.buy_success;
				String msg = "";
				if ("-1".equals(issueName)) {
					msg = "发起福彩3D" + " 购买方案\n\n我的认购：" + (allMoney * bs/*
																											 * -
																											 * canDeductMoney
																											 */)
							/*+ "元\n账户余额：" + ConvertJsonToList.getMap(js.getString("Data")).get("Balance").toString()/*
																													 * getBalance
																													 * (
																													 * )
																													 */
							+ "元"+"\n方案编号："+js.getString("Data").toString();

				} else {
					msg = "发起福彩3D" + issueName + "期购买方案\n\n我的认购："
							+ (allMoney * bs/* - canDeductMoney */)/* + "元\n账户余额："
							+ ConvertJsonToList.getMap(js.getString("Data")).get("Balance").toString()/*
																									 * getBalance
																									 * (
																									 * )
																									 */+ "元"+"\n方案编号："+js.getString("Data").toString();
				}

				new ShowTipDialog().showBuyInfo(Fc3dListActivity.this, msg, "投注成功", buy_icon, dyn[0]);
				clearPUtil();

				// 存贮数据
				MyPreference.getInstance(this).storeBLotID(LotConstants.LOT_ID_FC3D);
			}
		} catch (JSONException e) {
			// 异常处理代码
			Log.e("error", e.toString());
		}
	}

	/*
	 * 追号需要修改的地方
	 */
	public void Buy2(String strResult) {

		try {
			JSONTokener jsonParser = new JSONTokener(strResult);
			JSONObject js = (JSONObject) jsonParser.nextValue();
			if (Integer.parseInt(js.getString("Code")) < 0) {
				buy_icon = R.drawable.buy_failure;
				String balanceNotFull = js.getString("Data");
				// String balance = "当前余额:"+
				// ApplicationData.getInstance().getUserMoney() +"元" +"\n";

				JSONObject params = new JSONObject();
				params.put("Multiple", bs);
				params.put("LotteryID", lotID);
				params.put("IssueName", issueName);
				params.put("Money", bs * allMoney * zh);

				new ShowTipDialog().showBuyInfoZH(Fc3dListActivity.this, balanceNotFull, "购买失败", buy_icon,
						params.toString());
				/*Intent intent = new Intent();
				intent.setClass(context, JddRechargeListActivity.class);
				intent.putExtra("params", params.toString());
				startActivityForResult(intent, REQ_CODE_NO_MONEY_2_RECHARGE);*/


			} else {
				buy_icon = R.drawable.buy_success;

				String msg = "";

				System.err.println("期数：===" + zh);

				msg = "成功连续追号" + zh + "期\n\n我的认购：" + (allMoney * bs * zh)
						+ "元";//\n账户余额：" + js.getString("Msg").toString() + "元";

				if (!isFinishing()) {
					new ShowTipDialog().showBuyInfoZH(Fc3dListActivity.this, msg, "追号成功", buy_icon, dyn[0]);
					clearPUtil();
				}
				// 存贮数据
				MyPreference.getInstance(this).storeBLotID(LotConstants.LOT_ID_SSQ);
			}
		} catch (JSONException ex) {
			// 异常处理代码
			Log.e("error", ex.toString());
		}
	}

	public String getBalance() {
		String money = "";
		String strResult = null;
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			// 设置请求所需参数
			JSONObject params = new JSONObject();
			// 设置请求action
			String action = "107";// 108获取用户基本信息
			// 获取请求返回值
			String UserID = ApplicationData.getInstance().getUserID();
			strResult = GetRequestParams.getRequestUserIDResult(params.toString(), UserID, action,
					getApplicationContext());
			JSONTokener jsonParser = new JSONTokener(strResult);
			JSONObject js = (JSONObject) jsonParser.nextValue();
			map = ConvertJsonToList.getMap(js.getString("Data"));
			// money =
			// Float.parseFloat(map.get("Balance").toString().replaceAll(",",
			// "").trim());
			money = map.get("Balance").toString();
		} catch (Exception ex) {
			// TODO Auto-generated catch block
			Log.e("error", ex.toString());
		}
		return money;
	}

	/**
	 * 拼接投注号码
	 * 
	 * @return
	 */
	public List<Map<String, Object>> getLotNum() {
		String lotNum = "";
		List<BallDTO> arrayList = ApplicationData.getInstance().getList_pl3dto();
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (BallDTO elvdto : arrayList) {
			String elv_ball = elvdto.getPl3_ball();
			elv_ball = elv_ball.substring(0, elv_ball.lastIndexOf("-"));
			elv_ball = elv_ball.replaceAll(" ", "").trim();
			// Log.e("getLotNum1ss", "elv_ball= "+elv_ball.toString());
			if (playTypeId == 602) {
				String[] tempLotNum = elv_ball.split("\\|");
				for (int i = 0; i < tempLotNum.length; i++) {
					// 处理复式
					if (tempLotNum[i].length() > 1) {
						lotNum = lotNum + "(" + tempLotNum[i] + ")";
					} else {
						lotNum = lotNum + tempLotNum[i];
					}
				}
				lotNum = lotNum + "&";
			}
			if (playTypeId == 604 || playTypeId == 605) {
				lotNum = lotNum + elv_ball.replace(",", " ") + "&";
			}
		}

		lotNum = lotNum.substring(0, lotNum.lastIndexOf("&"));
		// Log.v("getLotNum", "lotNum= "+lotNum.toString());
		// 处理拆分数据
		if (playTypeId == 602) {// 直选，
			String number[] = lotNum.split("&");
			Map<String, Object> map = null;
			for (String num : number) {
				// Log.i("getLotNum", "num= "+num.toString());
				// Log.i("getLotNum", "num.length()= "+num.length());
				map = new HashMap<String, Object>();
				if (num.length() > 3) {
					map.put("playid", 602);
				} else {
					map.put("playid", 601);
				}
				map.put("number", num);
				list.add(map);
			}
		}
		// 组选3 ，组选6
		if (playTypeId == 604 || playTypeId == 605) {
			String number[] = lotNum.split("&");
			Map<String, Object> map = null;
			for (String num : number) {
				// Log.i("getLotNum", "num= "+num.toString());
				map = new HashMap<String, Object>();
				map.put("playid", playTypeId);
				map.put("number", num);
				list.add(map);
			}
		}
		// Log.v("getLotNum", "list= "+list.toString());
		return list;
	}

	// 随机取球，并显示
	public int[] RandomGenData(int min, int max, int num) {
		int[] temp;
		Random rndNumber;
		rndNumber = new Random();
		temp = new int[num];
		List<Integer> a1 = new ArrayList<Integer>();

		for (int i = min; i <= max; i++) {
			a1.add((Integer) i);
		}
		for (int i = 0; i < num; i++) {
			int index = rndNumber.nextInt(max - min + 1 - i);
			Object obj = a1.get(index);
			temp[i] = ((Integer) obj).intValue();
			a1.remove(index);
		}
		Arrays.sort(temp);
		return temp;
	}

	// 设置无记录
	public void setNoMsg() {
		List<Map<String, Object>> dataNull = new ArrayList<Map<String, Object>>();
		Map<String, Object> valueMap = new HashMap<String, Object>();
		valueMap.put("Message", "无记录");
		dataNull.add(valueMap);
		SimpleAdapter noMsgAdapter = new SimpleAdapter(Fc3dListActivity.this, dataNull, R.layout.buy_new11x5_listnum,
				new String[] { "Message" }, new int[] { R.id.buy_11x5_list_ball });
		setListAdapter(noMsgAdapter);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void initSimpleAdapter(ArrayList list) {
		// DelButtonAdapter listAdapter = new DelButtonAdapter(
		// Fc3dListActivity.this, list, R.layout.buy_new11x5_listnum,
		// new String[] { "buy_11x5_list_ball", "buy_11x5_list_money",
		// "buy_11x5_list_delete" }, new int[] {
		// R.id.buy_11x5_list_ball, R.id.buy_11x5_list_money,
		// R.id.buy_11x5_list_delete });
		NewDelButtonAdapter listAdapter = new NewDelButtonAdapter(Fc3dListActivity.this, list,
				R.layout.buy_new11x5_listnum, new String[] { "buy_11x5_list_ball", "buy_11x5_list_money",
						"buy_11x5_list_delete", "buy_11x5_list_playtype" }, new int[] { R.id.buy_11x5_list_ball,
						R.id.buy_new11x5_list_zhumoney, R.id.buy_11x5_list_delete, R.id.buy_new11x5_list_playtype });
		if (listAdapter.GetPl3List().size() > 0) {
			list = new ArrayList<HashMap<String, Object>>();
			money_dintInt = new DynArrayInt();
			Iterator<BallDTO> it = ApplicationData.getInstance().getList_pl3dto().iterator();
			for (; it.hasNext();) {
				it.next();
				it.remove();
			}
			for (BallDTO elvdto : listAdapter.GetPl3List()) {
				HashMap<String, Object> map = new HashMap<String, Object>();
				String[] ballString = elvdto.getPl3_ball().toString().split("-");
				map.put("buy_11x5_list_ball", ballString[0].toString().replace(",", " "));
				map.put("buy_11x5_list_money", ballString[1].toString());
				map.put("buy_11x5_list_delete", R.drawable.default_delbtn02);
				list.add(map);

				elvdto = new BallDTO();
				elvdto.setPl3_ball(ballString[0].toString().replace(",", " ") + "-" + ballString[1].toString());

				ApplicationData.getInstance().getList_pl3dto().add(elvdto);

				// 通过传来的单注金额，保存在money_dintInt数组里,计算出总金额
				money_dintInt.addInt(Integer.parseInt(ballString[1]));
				FactoryAllResult();
			}
			setListAdapter(listAdapter);
		} else {
			allMoney = 0;
			bs = 1;
			initText();
			// 获得自定义的应用程序BallDTO,初始化
			ApplicationData.getInstance().initList();
			setNoMsg();
			buy_pl3xhlb_linearlayout01.setVisibility(View.GONE);
			buy_pl3xhlb_linearlayout02.setVisibility(View.VISIBLE);
		}

	}

	private ArrayList<HashMap<String, Object>> getData(String elv_ball) {
		if (!elv_ball.equals("")) {
			String[] ballString = elv_ball.split("-");
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("buy_11x5_list_ball", ballString[0].toString().replace(",", " "));
			map.put("buy_11x5_list_money", ballString[1].toString());
			map.put("buy_11x5_list_delete", R.drawable.default_delbtn02);
			// 玩法
			map.put("buy_11x5_list_playtype", New11x5Util.getNewFC3DPlayType(playTypeId));
			list.add(map);
		}
		return list;
	}

	public void initView() {

		buy_pl3xhlb_linearlayout01 = findViewById(R.id.buy_elvxhlb_linearlayout01);
		buy_pl3xhlb_linearlayout02 = findViewById(R.id.buy_elvxhlb_linearlayout02);
		/*
		 * 追号需要修改的地方
		 */
		zhuihao_EditText = (EditText) findViewById(R.id.buy_ssqxhlb_zhuihao);
		btn_buy_fc3dhemai = (Button) findViewById(R.id.btn_buy_fc3dhemai);
		/*
		 * 追号的焦点事件
		 */
		zhuihao_EditText.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					yincangyu2 = (LinearLayout) findViewById(R.id.yicangyu2);
					yincangyu2.setVisibility(View.GONE);
					wuqi = (TextView) findViewById(R.id.wuqi);
					shiqi = (TextView) findViewById(R.id.shiqi);
					shiwuqi = (TextView) findViewById(R.id.shiwuqi);
					if (zh > 1) {
						yincangyu1.setVisibility(View.VISIBLE);
					}
					wuqi.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							zh = 5;
							zhuihao_EditText.setText("5");
						}
					});
					shiqi.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							zh = 10;
							zhuihao_EditText.setText("10");
						}
					});
					shiwuqi.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							zh = 15;
							zhuihao_EditText.setText("15");
						}
					});
				} else {
					yincangyu2.setVisibility(View.GONE);
					if (yincangyu1.VISIBLE == View.VISIBLE) {
						yincangyu1.setVisibility(View.GONE);
					}

				}

			}
		});
		zhuihao_EditText.addTextChangedListener(zhuihaoWatcher);
		buy_pl3xhlb_et = (EditText) findViewById(R.id.buy_11x5xhlb_EditText01);
		// 倍数的焦点事件
		buy_pl3xhlb_et.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					yincangyu1 = (LinearLayout) findViewById(R.id.yincangyu1);
					yincangyu1.setVisibility(View.GONE);
					yincangyu2 = (LinearLayout) findViewById(R.id.yicangyu2);
					yincangyu2.setVisibility(View.GONE);
				}

			}
		});
		moneyText = (TextView) findViewById(R.id.buy_11x5_money);
		initText();
		initButton();
		titleBar();
		iszhuihao = (CheckBox) findViewById(R.id.idzhuihao);

	}

	public void initText() {
		// bsText = (TextView) findViewById(R.id.buy_11x5_list_bs);

		// sumBsText = (TextView) findViewById(R.id.buy_11x5_bs);
		// countText = (TextView) findViewById(R.id.buy_11x5_count);

		buy_pl3xhlb_et.setText("1");
		// countText.setText("0");
		moneyText.setText("0");
		zhuihao_EditText.setText("1");
		// sumBsText.setText("1");
	}

	public void initButton() {
		btnAddRandom = (LinearLayout) findViewById(R.id.buy_11x5_list_addrandom);
		btnBack = (LinearLayout) findViewById(R.id.buy_11x5_list_back);
		btnClear = (LinearLayout) findViewById(R.id.buy_11x5_list_clear);
		// btnPlus = (Button) findViewById(R.id.buy_11x5_list_plus);
		// btnMinus = (Button) findViewById(R.id.buy_11x5_list_minus);
		btnBuy = (Button) findViewById(R.id.buy_11x5_buy);
		LinearLayout showOrHideLayout = (LinearLayout) findViewById(R.id.ll_show_hide);
//		// IssueID 幸运选号 ""
//		int isFromLucky = getIntent().getIntExtra("isfromlucky", 0);
//		if (isFromLucky == 1) {
//			btn_buy_fc3dhemai.setVisibility(View.GONE);
//			showOrHideLayout.setVisibility(View.GONE);
//			findViewById(R.id.rlzhuihao).setVisibility(View.INVISIBLE);
//		} else {
//			btn_buy_fc3dhemai.setVisibility(View.VISIBLE);
//			showOrHideLayout.setVisibility(View.VISIBLE);
//			// findViewById(R.id.rlzhuihao).setVisibility(View.VISIBLE);
//		}
	}

	// 计算注数和金额
	public void FactoryAllResult() {
		// 计算注数和金额
		int money = 0;
		for (int i = 0; i < money_dintInt.getSize(); i++) {
			money = money + money_dintInt.getInt(i);
		}
		allMoney = money;
		// bs = Integer.parseInt(buy_11x5xhlb_EditText01.getText().toString());
		ApplicationData.getInstance().setAll_pl3money(allMoney);
		// countText.setText(money / 2 + "");
		moneyText.setText(money * bs * zh + "");
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (requestCode == REQ_CODE_BUYMAIN && resultCode == RESULT_OK) {
			// onKeyDown(KeyEvent.KEYCODE_BACK, null);
			// 获得自定义的应用程序BallDTO,初始化
			ApplicationData.getInstance().initList();
			setResult(RESULT_OK);
			Fc3dListActivity.this.finish();
		}

		if (resultCode == RESULT_OK) {
			if (requestCode == DefConstants.REQ_CODE_NO_MONEY_2_RECHARGE) {
				ApplicationData.getInstance().initList();
				setResult(RESULT_OK);
				finish();
			}
		}
	};

	// TitleBar标题栏
	public void titleBar() {
		// Title信息(返回+Title)
		Button back = (Button) findViewById(R.id.top_title_back);
		back.getBackground().setAlpha(255);// 透明度0~255透明度值 ，值越小越透明
		back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onKeyDown(KeyEvent.KEYCODE_BACK, null);
			}
		});

		TextView info = (TextView) findViewById(R.id.top_title_info);
		info.setText("福彩3D");// R.string.fucai3d_list_name

		Button refres = (Button) findViewById(R.id.top_title_refres);
		refres.getBackground().setAlpha(255);
		refres.setVisibility(View.INVISIBLE);
		// refres.setBackgroundResource(R.drawable.default_menu);
		// refres.setVisibility(View.VISIBLE);
		refres.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

			}
		});
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (ApplicationData.getInstance().getList_pl3dto().size() != 0) {
				MyDialog.Builder dialog = new MyDialog.Builder(context);
				dialog.setTitle("退出提示");
				dialog.setMessage("是否清除本次选号?");
				dialog.setPositiveButton("清除", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						clearPUtil();

						// 获得自定义的应用程序BallDTO,初始化
						ApplicationData.getInstance().initList();
						dialog.cancel();
						finish();
					}
				});
				dialog.setNegativeButton("保存", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						clearPUtil();
						int size = ApplicationData.getInstance().getList_pl3dto().size();
						pUtil.putInt("fc3d_size", size);
						for (int i = 0; i < size; i++) {
							pUtil.putString("fc3d_ball" + i, ApplicationData.getInstance().getList_pl3dto().get(i)
									.getPl3_ball());
						}
						pUtil.putInt("fc3d_allMoney", ApplicationData.getInstance().getAll_pl3money());
						pUtil.putInt("fc3d_play", playTypeId);
						ApplicationData.getInstance().initList();
						dialog.cancel();
						finish();
						Toast.makeText(context, R.string.design_change_saved, Toast.LENGTH_SHORT).show();
					}
				}).create().show();
			} else {
				// 无对话框退出
				clearPUtil();
				ApplicationData.getInstance().initList();
				finish();
			}
		} else {
			return super.onKeyDown(keyCode, event);
		}
		return true;
	}

	// 清除保存的号码记录
	public void clearPUtil() {
		int size = pUtil.getInt("fc3d_size", 0);
		if (size != 0) {
			for (int i = 0; i < size; i++) {
				pUtil.remove("fc3d_ball" + i);
			}
			pUtil.remove("fc3d_size");
			pUtil.remove("fc3d_allMoney");
			pUtil.remove("fc3d_play");
		}
	}

	@Override
	public void finish() {
		// TODO Auto-generated method stub
		super.finish();
		SysApplication.getInstance().removeActivity(this);
	};
}
